package com.wwfish.cmsui.modules.common.client.util;

import com.google.gwt.user.client.ui.ListBox;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.ui.textbox.FishListBox;
import com.wwfish.cms.model.GeneralCodeDto;
import com.wwfish.cms.service.CodeManager;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-9
 * Time: 10:21:06
 * To change this template use File | Settings | File Templates.
 */
public class CodesHelper {
    private static Map<String, List> cache = new HashMap();

    public static void getCodesById(final Long parentId, final IAsyncModelCallback callback) {
        if (cache.get(parentId) == null)
            ServiceFactory.invoke(CodeManager.class.getName(), "getCodesByParentId", new Object[]{parentId}, new FishAsyncCallback() {
                public void onSuccess(Object o) {
                    cache.put(parentId.toString(), (List) o);
                    if (callback != null)
                        callback.setModelElments(o);
                }
            });
        else if (callback != null)
            callback.setModelElments(cache.get(parentId.toString()));

    }

    public static void cacheCodesById(final Long parentId) {
        if (cache.get(parentId) == null)
            ServiceFactory.invoke(CodeManager.class.getName(), "getCodesByParentId", new Object[]{parentId}, new FishAsyncCallback() {
                public void onSuccess(Object o) {
                    cache.put(parentId.toString(), (List) o);
                }
            });
    }

    public static List getCodesById(Long id) {
        return cache.get(id.toString());
    }

    public static String getNameByCode(List codes, String code) {
        for (Iterator it = codes.iterator(); it.hasNext();) {
            GeneralCodeDto temp = (GeneralCodeDto) it.next();
            if (temp.getCode().equals(code))
                return temp.getName();
        }
        return code;
    }

    public static String getNameByCode(Long parentId, String code) {
        List codes = cache.get(parentId.toString());
        if (codes != null)
            for (Iterator it = codes.iterator(); it.hasNext();) {
                GeneralCodeDto temp = (GeneralCodeDto) it.next();
                if (temp.getCode().equals(code))
                    return temp.getName();
            }
        return code;
    }

    public static void initGeneralCodesToListWidget(List<GeneralCodeDto> codes, FishListBox listWidget) {
        if (codes != null && codes.size() > 0) {
            for (int i = 0; i < codes.size(); i++) {
                GeneralCodeDto category = codes.get(i);
                listWidget.addItem(category.getName(), category.getCode());
            }
        }
    }

    public static void initGeneralCodesToListWidget(List<GeneralCodeDto> codes, ListBox listWidget, boolean isNull) {
        if (isNull) listWidget.addItem("-请选择-", "");
        if (codes != null && codes.size() > 0) {
            for (int i = 0; i < codes.size(); i++) {
                GeneralCodeDto category = codes.get(i);
                listWidget.addItem(category.getName(), category.getCode());
            }
        }
    }

}